package client;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

import javax.swing.*;

public class Confermaturno extends JFrame implements ActionListener,Serializable {

	private JButton ok;
	private JButton passa;
	
	private int risp ;
	
	public Confermaturno(){
		
		setLayout(new GridLayout(2,1));
		
		
		JPanel pannello1 = new JPanel();
		pannello1.setLayout(new GridLayout(1,2));
		JPanel pannello2 = new JPanel();
		pannello2.setLayout(new FlowLayout());
		
		
		ok = new JButton("OK");
		ok.setPreferredSize(new Dimension(50,20));
		ok.addActionListener(this);
		pannello1.add(ok);
		
		passa = new JButton("Passa");
		passa.setPreferredSize(new Dimension(50,20));
		passa.addActionListener(this);
		pannello1.add(passa);
		
		JLabel messaggio = new JLabel("E' il tuo turno! Conferma (OK) oppure passa (Passa)");
		pannello2.add(messaggio);
		
		add(pannello2);
		add(pannello1);
		pack();
		validate();
		setVisible(true);			
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		synchronized(this){
			if(e.getSource().equals(ok)){
				risp=1;
				dispose();
				notifyAll();
			}
		}
		
		synchronized(this){
			if(e.getSource().equals(passa)){
				risp=2;
				dispose();
				notifyAll();
			}
		}
		
	}
	
	public int getRisp(){
		synchronized(this){
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return risp;
	}

	public void chiudi(){
		dispose();
	}
}
